

package com.zidian.leader.common.adapter;


import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


import com.zidian.leader.R;
import com.zidian.leader.common.listener.OnAdvancedRecycleViewItemLongClickListener;
import com.zidian.leader.common.listener.OnAdvancedRecyclerViewItemListener;
import com.zidian.leader.common.listener.OnAdvancedRecyclerViewLoadMoreListener;
import com.zidian.leader.common.view.AdvancedRecyclerView;

import java.util.ArrayList;
import java.util.List;

/**
 * 自定义 AdvancedRecyclerView 的通用 Adapter
 * Created by Zuo Ji on 2015/7/28 0028.
 */
public class AdvancedRecyclerViewAdapter<T> extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private List<T> data;

    public Context context;

    // 滚动 Item 类型：普通
    public static final int TYPE_ITEM = 0;
    // 滚动 Item 类型：底部
    public static final int TYPE_FOOTER = 1;

    // 加载更多标示：普通正在加载中
    private static final int LOAD_MORE_WAITING = 0;
    // 加载更多标示：没有加载到数据
    private static final int LOAD_MORE_NO_DATA = 1;
    // 加载更多标示：加载更多重试入口
    private static final int LOAD_MORE_TRY_AGAIN = 2;

    // recyclerView 的类型
    private String recyclerViewType;

    /**
     * 加载更多状态标示 默认为正在加载
     */
    private int loadMoreStatus = LOAD_MORE_TRY_AGAIN;
    /**
     * item 点击监听
     */
    private OnAdvancedRecyclerViewItemListener onItemClickListener;
    /**
     * FooterView 点击事件
     */
    private OnAdvancedRecyclerViewLoadMoreListener onLoadMoreListener;

    /**
     * Item 长按监听
     */
    private OnAdvancedRecycleViewItemLongClickListener onItemLongClickListener;

    /**
     * 适配器上显示的数据集合
     *
     * @return 数据集合
     */
    public List<T> getData() {
        return data;
    }

    /**
     * 设置 Item 点击监听
     *
     * @param onItemClickListener 点击监听
     */
    public void setOnItemClickListener(OnAdvancedRecyclerViewItemListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    /**
     * 获取 Item 点击监听事件
     */
    public OnAdvancedRecyclerViewItemListener getOnItemClickListener() {
        return onItemClickListener;
    }

    /**
     * FooterView 点击事件
     */
    public OnAdvancedRecyclerViewLoadMoreListener getOnLoadMoreListener() {
        return onLoadMoreListener;
    }

    /**
     * FooterView 点击事件
     *
     * @param onLoadMoreListener 点击监听
     */
    public void setOnLoadMoreListener(OnAdvancedRecyclerViewLoadMoreListener onLoadMoreListener) {
        this.onLoadMoreListener = onLoadMoreListener;
    }

    /**
     * 设置 Item 长按监听
     *
     * @param onItemLongClickListener 长按监听
     */
    public void setOnItemLongClickListener(OnAdvancedRecycleViewItemLongClickListener onItemLongClickListener) {
        this.onItemLongClickListener = onItemLongClickListener;
    }

    /**
     * 获取 item 的长按监听
     */
    public OnAdvancedRecycleViewItemLongClickListener getOnItemLongClickListener() {
        return onItemLongClickListener;
    }

    /**
     * 获取加载更多状态标示
     *
     * @return 加载更多状态
     */
    public int getLoadMoreStatus() {
        return loadMoreStatus;
    }

    /**
     * 设置加载更多状态标示
     *
     * @param loadMoreStatus 加载更多状态
     */
    public void setLoadMoreStatus(int loadMoreStatus) {
        this.loadMoreStatus = loadMoreStatus;
    }

    /**
     * 获取 RecyclerView 的视图类型
     *
     * @return 视图类型
     */
    public String getRecyclerViewType() {
        return recyclerViewType;
    }

    /**
     * 设置 RecyclerView 的视图类型
     *
     * @param recyclerViewType 视图类型
     */
    public void setRecyclerViewType(String recyclerViewType) {
        this.recyclerViewType = recyclerViewType;
    }

    /**
     * 构造函数
     *
     * @param context 上下文
     */
    public AdvancedRecyclerViewAdapter(Context context, List<T> data) {
        this.context = context;
        this.data = data;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == TYPE_FOOTER) {
            View footerView = LayoutInflater.from(context).inflate(R.layout.recycler_view_loadmore, null);
            footerView.setLayoutParams(new RecyclerView.LayoutParams(RecyclerView.LayoutParams.MATCH_PARENT,
                                                                     RecyclerView.LayoutParams.WRAP_CONTENT));
            return new AdvancedRecyclerViewHolder(footerView, viewType, getOnItemClickListener(),
                                                  getOnLoadMoreListener(), getOnItemLongClickListener());
        }
        return null;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int position) {
        AdvancedRecyclerViewHolder holder = (AdvancedRecyclerViewHolder) viewHolder;
        holder.position = position;

        if (getItemViewType(position) == TYPE_FOOTER) { // item 为 Footer 视图时
            switch (getLoadMoreStatus()) {
                case LOAD_MORE_WAITING:
                    holder.loadMoreTextView.setText("加载中...");
                    holder.loadMoreProgressBar.setVisibility(View.VISIBLE);
                    break;
                case LOAD_MORE_NO_DATA:
                    holder.loadMoreTextView.setText("已加载全部");
                    holder.loadMoreProgressBar.setVisibility(View.GONE);
                    break;
                case LOAD_MORE_TRY_AGAIN:
                    holder.loadMoreTextView.setText("加载更多");
                    holder.loadMoreProgressBar.setVisibility(View.GONE);
                    break;
            }
        }
    }

    @Override
    public int getItemCount() {
        if (getOnLoadMoreListener() != null && data.size() != 0) {
            return data.size() + 1;
        }
        return data.size();
    }

    @Override
    public int getItemViewType(int position) {
        // 最后一个item设置为footerView
        if (getOnLoadMoreListener() != null && position + 1 == getItemCount()) {
            return TYPE_FOOTER;
        } else {
            return TYPE_ITEM;
        }
    }

    /**
     * 公开方法：获取某个 item 上的实例
     *
     * @param position item 位置
     * @return 实例
     */
    public T getItem(int position) {
        return data.get(position);
    }

    /**
     * 公开方法：获取最后一个 item 上的实例
     *
     * @return 实例
     */
    public T getLastItem() {
        if (getOnLoadMoreListener() != null) {
            return data.get(getItemCount() - 2);
        } else {
            return data.get(getItemCount() - 1);
        }
    }

    /**
     * RecyclerView 调用：加载更多 没有加载到数据时调用
     */
    public void onLoadMoreNoData() {
        setLoadMoreStatus(LOAD_MORE_NO_DATA);
        notifyItemChanged(getItemCount() - 1);
    }

    /**
     * RecyclerView 调用：加载更多 加载更多重试入口
     */
    public void onLoadMoreTryAgain() {
        setLoadMoreStatus(LOAD_MORE_TRY_AGAIN);
        notifyItemChanged(getItemCount() - 1);
    }

    /**
     * RecyclerView 调用：加载更多 加载更多
     */
    public void onLoadMoreWaiting() {
        setLoadMoreStatus(LOAD_MORE_WAITING);
        notifyItemChanged(getItemCount() - 1);
    }

    /**
     * 是否能够加载历史
     *
     * @return true ：有加载历史的功能； false：没有
     */
    public boolean canLoadMore() {
        if (recyclerViewType.equals(AdvancedRecyclerView.LAYOUT_MANAGER_LINEAR_TAG) && getOnLoadMoreListener() != null) {
            return true;
        }
        return false;
    }

    /**
     * RecyclerView 调用：将刷新到的数据加载到列表中显示
     *
     * @param refreshData 刷新到的数据
     */
    public void refreshData(List<T> refreshData) {
        if (data == null || data.size() == 0) {
            loadMoreData(refreshData);
        } else {
            data.clear();
            data.addAll(refreshData);
            notifyDataSetChanged();
        }
    }

    /**
     * RecyclerView 调用：将加载更多获取到的数据加载到列表中显示
     *
     * @param loadMoreData 加载更多获取到的数据
     */
    public void loadMoreData(List<T> loadMoreData) {
        if (data == null) {
            data = new ArrayList<T>();
        }
        data.addAll(loadMoreData);
        notifyDataSetChanged();
    }

    /**
     * RecyclerView 调用：刷新某个 Item 视图
     *
     * @param position 需要刷新的位置
     */
    public void refreshItemView(int position) {
        notifyItemChanged(position);
    }
}
